package com.yricky.memoria.ui.component

import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material.Text
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.Send
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.sp
import com.yricky.memoria.ui.component.common.*
import com.yricky.memoria.ui.theme.ThemeInst
import com.yricky.memoria.utils.PdfDoc
import com.yricky.memoria.utils.RStr
import com.yricky.memoria.utils.openPdfExternal

/**
 * @author Yricky
 * @date 2021/11/27
 */

@Composable
fun PdfActivityContent(viewModel: PdfViewModel){
    ThemeInst.Material {
        val viewState by viewModel.viewState.collectAsState()
        val context = LocalContext.current
        Column(modifier = Modifier.fillMaxSize()) {
            Title(viewState.title){
                viewState.query?.let { query->
                    MenuItems.Item(
                        state = it,
                        icon = Icons.Rounded.Send,
                        text = RStr.stub("用外部应用打开")
                    ) {
                        context.openPdfExternal(query)
                    }
                }

            }
            LazyColumn{
                viewState.doc?.let{ doc ->
                    items(doc.pageCount) {
                        PdfItem(pdfDoc = doc, index = it)
                    }
                }

            }
        }
    }
}

@Composable
fun PdfItem(pdfDoc: PdfDoc,index:Int){
    val context = LocalContext.current
    ItemContentFrame(
        content = {
        Box(modifier = Modifier.wrapContentSize(),
            contentAlignment = Alignment.Center){
            PdfPreview( pdf = pdfDoc, pageIndex = index,
                width = context.resources.displayMetrics.widthPixels,
                height = context.resources.displayMetrics.heightPixels
            )
            Text(
                text = "$index",
                fontSize = 10.sp,
                modifier = Modifier.align(Alignment.BottomStart)
            )
        }
    })
}